/*
	File: D:/works_2/panzer_2010/data/scripts/saucer_body_follow_target.nut
	Author: The Team
*/

		Include("data/scripts/cannon.nut")
		Include("data/scripts/game_item.nut")		
		
class	Enemy	extends GameItem
{

	energy		=	1.0
	alive		=	true
	audio		=	0

	scene		=	0
	
	function	OnSetup(item)
	{
		base.OnSetup(item)

		print("Enemy::OnSetup()")
		scene = ItemGetScene(item)
		ItemSetCollisionMask(item, with_mask.enemy)
		ItemSetSelfMask(item, self_mask.enemy)

		local	project_script = ProjectGetScriptInstance(g_project)
		audio = project_script.audio.enemy
	}

	function	OnUpdate(item)
	{
		base.OnUpdate(item)

		if (alive)
		{
			if (energy <= 0.0)
			{
				alive	=	false
				Explode(item)
			}
		}
	}

	function	OnCollision(item, with_item)
	{
		if (ItemGetName(with_item) == "player_bullet")
			energy -= 0.1
	}

	function	Explode(item)
	{
		local	n, explosion, size, timer

		audio.PlaySfxExplode()

		//local	bbox = ItemGetMinMax(item)
		size = Mtr(0.5) //bbox.min.Dist(bbox.max)

		for(n = 0; n < 8; n++)
		{
			local	rand_vect = Vector(Rand(-1.0,1.0), 0.0, Rand(-1.0,1.0)).Normalize(size)
			rand_vect.y = Mtr(0.5)
			explosion = Explosion(scene, ItemGetWorldPosition(item) + rand_vect, 10.0 + Rand(2.5,5.0), n * 0.02)
		}

		Die(item)
	}

	function	Die(item)
	{
		//ItemSetPhysicMode(item, PhysicModeNone)
		ItemSetPosition(item, Vector(0, -5000, 0))
		ItemSetPhysicPosition(item, Vector(0, -5000, 0))
		//ItemActivateHierarchy(item, false)
		Disable(item)
	}
}